/**
 * 初始化加载数据，所有参数会赋初始值
 * @param {Object} _that 	页面vue对象，一般传递this
 * @param {Object} url 		加载数据连接地址
 * @param {Object} params 	查询参数，会放入缓存，以后查询都会带上
 */
const initData = function(_that, url, params) {
	let obj = {
		datas: [],
		page: 0,
		rows: getApp().globalData.pageNum,
		loadding: false,
		pullUpOn: true
	}
	if (url) {
		obj.url = url
	}
	if (params) {
		_that.commonData.query = params
	}
	if (!_that.commonData) {
		_that.commonData = {}
	}
	_that.commonData = Object.assign({}, _that.commonData, obj)
	return loadData(_that)
}
/**
 * 加载数据方法，参数解释参考initData方法
 * 注意：与initData中的params参数相同时，此时的查询参数会覆盖initData的参数值
 * @param {Object} _that 
 * @param {Object} params
 */
const loadData = function(_that, params) {
	let commonData = _that.commonData
	if (!commonData) {
		_that.om.toast("参数错误！");
		return new Promise((resolve, reject) => reject())
	}
	if (!params) {
		params = {}
	} else {
		if (commonData.query) {
			_that.commonData.query = Object.assign({}, commonData.query, params)
		}
	}
	params.page = commonData.page + 1
	params.rows = commonData.rows
	var method = commonData.method || 'get'
	if(_that.defaultData != "" && _that.hasOwnProperty("defaultData")){
		commonData.datas = _that.defaultData
		return
	}
	return _that.om.request(commonData.url,
		(commonData.query ? Object.assign({}, commonData.query, params) : params), method).then(res => {
		//if (res instanceof Array) {
		if(Object.prototype.toString.call(res)==='[object Array]'){
			if (res.length) {
				if (commonData.datas.length) {
					_that.commonData.datas = [...commonData.datas, ...res];
				} else if (!commonData.datas.length) {
					_that.commonData.datas = res;
				}
				_that.commonData.page = commonData.page + 1;
				if ((commonData.rows-res.length) > 0) {
					_that.commonData.pullUpOn = false;
				}
			} else {
				_that.commonData.pullUpOn = false;
			}
			return new Promise(resolve => resolve())
		} else {
			if(res.rows){
				if (res.rows.length) {
					if (commonData.datas.length) {
						_that.commonData.datas = [...commonData.datas, ...res.rows];
					} else if (!commonData.datas.length) {
						_that.commonData.datas = res.rows;
					}
					_that.commonData.page = commonData.page + 1;
					if ((commonData.rows-res.rows.length) > 0) {
						_that.commonData.pullUpOn = false;
					}else if(_that.commonData.datas.length >= res.total){
						_that.commonData.pullUpOn = false;
					}
				} else {
					_that.commonData.pullUpOn = false;
				}
				return new Promise(resolve => resolve())
			}else{
				if (res.succeed) {
					if (res.value && res.value.length) {
						if (commonData.datas.length) {
							_that.commonData.datas = [...commonData.datas, ...res.value];
						} else if (!commonData.datas.length) {
							_that.commonData.datas = res.value;
						}
						_that.commonData.page = commonData.page + 1;
						if ((commonData.rows-res.value.length) > 0) {
							_that.commonData.pullUpOn = false;
						}
					} else {
						_that.commonData.pullUpOn = false;
					}
					return new Promise(resolve => resolve())
				} else {
					_that.om.toast(res.msg);
					return new Promise((resolve, reject) => reject())
				}
			}
			
		}

	})
}


//页面相关事件处理函数--监听用户下拉动作
const pullDownRefresh = function(_that) {
	initData(_that).then(
		res => {
			uni.stopPullDownRefresh();
			_that.om.toast('刷新成功');
		},
		err => {
			uni.stopPullDownRefresh();
		}
	)
}

// 页面上拉触底事件的处理函数
const reachBottom = function(_that) {
	if (!_that.commonData.pullUpOn) return;
	if(!_that.commonData.loadding){
		_that.commonData.loadding = true;
		loadData(_that).then(res=>{
			_that.commonData.loadding = false;
		})
	}
}
// 页面上拉触底事件的处理函数（返回promise）
const reachBottomReturn = function(_that) {
	if (!_that.commonData.pullUpOn){
		let promise = new Promise(function(resolve, reject){ 
		 //异步操做
        resolve();
		});
		return promise;
	} 
	if(!_that.commonData.loadding){
		_that.commonData.loadding = true;
		return loadData(_that)
	}
}

export default {
	initData,
	loadData,
	pullDownRefresh,
	reachBottom,
	reachBottomReturn
}
